<template>
  <view class="webview">
    <web-view :src="src" @message="onMessage" />
  </view>
</template>

<script setup lang="ts">
import { ref } from "vue";
import Taro, { useLoad } from "@tarojs/taro";
import { apiAchieveMission } from "@/apis/knowledge";
import { IMissionRecordReq } from "@/apis/types/knowledge";

defineOptions({
  name: "web-view",
});

const src = ref("");

useLoad((params) => {
  const { url, title } = params;
  if (url) {
    src.value = decodeURIComponent(url);
    console.log(src.value);
  }
  if (title) {
    Taro.setNavigationBarTitle({
      title: decodeURIComponent(title) || "",
    });
  }
});

const onMessage = (e: any) => {
  const { data } = e.detail;
  const [event] = data;
  if (event.type === "test_submit") {
    const params: IMissionRecordReq = {
      actionType: "MISSION_EXAM",
      categoryId: null,
      mainId: new Date().getTime(),
      watchDuration: null,
      isFinished: 1,
    };
    apiAchieveMission(params);
  }
};
</script>

<style lang="less">
.webview {
}
</style>
